﻿using System;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DataAccess;
using NLog;

namespace QuanLyKhachSan
{
    public partial class TabThietBiPhong : XtraUserControl, IGeneatetabName
    {
        public string Getname()
        {
            return "Thiết bị phòng";
        }
        ThietBiPhongManager controller = new ThietBiPhongManager();
        BindingSource bs = new BindingSource();
        protected static Logger _logger = LogManager.GetCurrentClassLogger();

        public TabThietBiPhong()
        {
            InitializeComponent();
        }
        private void TabThietBiPhong_Load(object sender, EventArgs e)
        {
            Loading();
        }

        private void Loading()
        {
            if (!controller.LayDanhSach().Result.Any())
            {
                string s = controller.LayDanhSach().Exceptions.First().Message;
                MessageBox.Show(s);
                _logger.Error(s);
            }
            else
            {
                bs.DataSource = controller.LayDanhSach().Result;
                dgvThietBiPhong.DataSource = bs;
                for (int i = 6; i < dgvThietBiPhong.Columns.Count; i++)
                {
                    dgvThietBiPhong.Columns[i].Visible = false;
                }
            }
        }

        private void dgvThietBiPhong_Click(object sender, EventArgs e)
        {
            try
            {
                int selectedRow = TimDong();
                if (selectedRow >= 0)
                {
                    toolStripStatusLabel2.Text = dgvThietBiPhong.Rows[selectedRow].Cells[0].Value + "";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                _logger.Error(ex.Message);
            }

        }
        private int TimDong()
        {
            for (int i = 0; i < dgvThietBiPhong.Rows.Count; i++)
            {
                if (dgvThietBiPhong.Rows[i].Selected)
                {
                    return i;
                }
            }
            return -1;
        }

        private int TimDong(string ma)
        {
            for (int i = 0; i < dgvThietBiPhong.Rows.Count; i++)
            {
                if (dgvThietBiPhong.Rows[i].Cells[0].Value + "" == ma)
                {
                    return i;
                }
            }
            return -1;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            bool them = true;
            _logger.Info(Commons.ToString() + ",Chọn thao tác thêm thiết bị phòng");

            frmThietBiPhong frm = new frmThietBiPhong(them);
            frm.ShowDialog();
            if (frm.tbp != null)
            {
                if (controller.LayDanhSach().Result.Count <= 1)
                {
                    Loading();
                }
                else
                {
                    bs.Add(frm.tbp);
                    dgvThietBiPhong.Rows[TimDong(frm.tbp.MaTB)].Selected = true;
                }
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            _logger.Info(Commons.ToString() + ",Chọn thao tác xóa thiết bị phòng");

            DialogResult kq = MessageBox.Show("Bạn có muốn xóa dữ liệu này không?", "CẢNH BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (kq == DialogResult.Yes)
            {
                var aResult = controller.Xoa(toolStripStatusLabel2.Text);
                if (!aResult.Exceptions.Any())
                {
                    MessageBox.Show("Xóa thành công thiết bị phòng ID= " + toolStripStatusLabel2.Text);
                    bs.RemoveAt(TimDong(toolStripStatusLabel2.Text));
                    dgvThietBiPhong.Rows[0].Selected = true;
                    _logger.Info(Commons.ToString() + ", Xóa thành công thiết bị phòng ID= " + toolStripStatusLabel2.Text);
                }
                else
                {
                    string s = "";
                    foreach (var ex in aResult.Exceptions)
                    {
                        s += ex.Message + "\n";
                    }
                    MessageBox.Show(s);
                    _logger.Error(s);
                }
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            _logger.Info(Commons.ToString() + ",Chọn thao tác sửa thiết bị phòng");

            if (toolStripStatusLabel2.Text != "" && controller.LayDanhSach().Result.Count > 0)
            {
                ThietBiPhong tbpold = (ThietBiPhong)bs[TimDong(toolStripStatusLabel2.Text)];
                frmThietBiPhong frm = new frmThietBiPhong(tbpold);
                frm.ShowDialog();
                if (frm.tbp != null)
                {
                    bs.RemoveAt(TimDong(tbpold.MaTB));
                    bs.Add(frm.tbp);
                    dgvThietBiPhong.Rows[TimDong(frm.tbp.MaTB)].Selected = true;
                }
            }
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            _logger.Info(Commons.ToString() + ",Chọn thao tác tìm kiếm thiết bị phòng");

            frmTimKiemThietBiPhong frm = new frmTimKiemThietBiPhong();
            frm.ShowDialog();
            if ((frm.rdName != "" && frm.thongtintimkiem != "") || frm.rdName == "rdFindAll")
            {
                switch (frm.rdName)
                {
                    case "rdFindByRoomType":
                        var aResult = controller.TimKiemTheoDK(tbp => tbp.MaLoaiPhong == frm.thongtintimkiem);
                        if (!aResult.Exceptions.Any())
                        {
                            bs.DataSource = aResult.Result;
                        }
                        else
                        {
                            string s = "";
                            foreach (var ex in aResult.Exceptions)
                            {
                                s += ex.Message + "\n";
                            }
                            MessageBox.Show(s);
                        }
                        break;
                    case "rdFindAll":
                        bs.DataSource = controller.LayDanhSach().Result;
                        break;
                }
            }
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            _logger.Info(Commons.ToString() + ",Chọn thao tác in danh sách thiết bị phòng");

            frmInThietBiPhong frm = new frmInThietBiPhong();
            frm.ShowDialog();
        }

        private void ibtnThem_Click(object sender, EventArgs e)
        {
            btnThem.PerformClick();
        }

        private void ibtnXoa_Click(object sender, EventArgs e)
        {
            btnXoa.PerformClick();
        }

        private void ibtnCapNhat_Click(object sender, EventArgs e)
        {
            btnSua.PerformClick();
        }

        private void ibtnTimKiem_Click(object sender, EventArgs e)
        {
            btnTimKiem.PerformClick();
        }

        private void ibtnIn_Click(object sender, EventArgs e)
        {
            btnIn.PerformClick();
        }

        private void themTBPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnThem.PerformClick();
        }

        private void capnhatTBPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnSua.PerformClick();
        }

        private void xoaTBPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnXoa.PerformClick();
        }

        private void timkiemTBPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnTimKiem.PerformClick();
        }

        private void indsTBPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnIn.PerformClick();
        }
    }
}
